<?php
class offer
{
	var $db;
	var $tpl;
	var $page;
	var $pic_save_path;	 
	function offer()
	{
		global $db;
		global $config;
		global $tpl;		
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
		$this -> cTime  = date("Y-m-d H:i:s");
		$this->pic_save_path = "uploadfile/product/";					//图片保存目录
		if(!empty($_POST))
		{
			include_once($config['webroot'].'/includes/filter_class.php');
			$word=new Text_Filter();
			$_POST['con']=$word->wordsFilter($_POST['con'], $matche_row);
			$_POST['detail']=$word->wordsFilter($_POST['detail'], $matche_row);
			$_POST['title']=$word->wordsFilter($_POST['title'], $matche_row);
		}
	}
	
	function add_info()
	{
		if(empty($_POST['catid'])&&empty($_POST['title'])&&empty($_POST['con']))
			return false;
		global $buid,$config;
		//===================================过滤<a
		include($config['webroot']."/config/usergroup.php");
		$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
		$my_group=$group[$ifpay]['modeu'];
		if($my_group['replace_outside_link']==1)
		{
			$_POST["con"]=replace_outside_link($_POST["con"]);
			$_POST["detail"]=replace_outside_link($_POST["detail"]);
		}
		//======================================
		$cpc=$this->get_user_pv($buid);
		
		if(empty($_POST['con']))
		   $con=substr(strip_tags($_POST["detail"]),0,100);
		else
		   $con=htmlspecialchars($_POST["con"]);
		   
		$type = $_POST["type"]<1?1:$_POST["type"];
		$title=htmlspecialchars($_POST["title"]);
		$catid=$_POST['catid'];
		if(!empty($_POST['tcatid']))
			$catid=$_POST['tcatid'];
		if(!empty($_POST['scatid']))
			$catid=$_POST['scatid'];
		if(!empty($_POST['sscatid']))
			$catid=$_POST['sscatid'];
		$cat_name=$this->readCatName($catid);
		
		//##=====================添加图片
		if(!$admin)
		{
			include_once("$config[webroot]/includes/admin_class.php");
			$admin = new admin();
		}
		$pic = $admin->add_pro_pic($POST['type']<1?true:false);

		$sql="insert into ".INFO."
		(userid,catid,cat_name,title,con,uptime,statu,market_price,price,priceDes,type,model,gg,pp,valid_time,pic,user,keywords,country,province,city,company,ifpay)
		values
		('$buid','$catid','$cat_name','$title','$con','$this->cTime','$my_group[infoCheck]','$_POST[market_price]','$_POST[price]','$_POST[unit]','$type','$_POST[model]','$_POST[gg]','$_POST[pinpai]','$_POST[validTime]','$pic','$_COOKIE[USER]','$_POST[keywords]','$cpc[country]','$cpc[province]','$cpc[city]','$cpc[company]','$ifpay')";
		$this->db->query($sql);
		$id=$this->db->lastid();
		$this->db->query("INSERT INTO ".OFFERDETAIL." (userid,offerid,detail) values ('$buid','$id','$_POST[detail]')");
		
		update_cat_nums($catid,'add','offer');
		//##======================
		if($_POST['custom_cat'])
		{
			$sql="insert into ".CUSTOM_CAT_REL." (custom_cat_id,custom_cat_type,relating_id) values 
			('$_POST[custom_cat]','2','$id')";
			$re=$this->db->query($sql);
		}
		//##======================
		include_once($config['webroot']."/includes/tag_inc.php");
		add_tags($_POST['keyword'],2,$id);
		include($config['webroot']."/config/point_config.php");
		if($point_config['point']=='1'&&$point_config['$offer_pic']!='0')
			renew_point('',$point_config['add_offer']);

		return $id;
	}
	function edit_info()
	{
		global $buid,$config;
		
		#过滤<a
		include($config['webroot']."/config/usergroup.php");
		$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
		$my_group=$group[$ifpay]['modeu'];
		if($my_group['replace_outside_link']==1)
		{
			$_POST["con"]=replace_outside_link($_POST["con"]);
			$_POST["detail"]=replace_outside_link($_POST["detail"]);
		}
		#过滤<a
		$title=htmlspecialchars($_POST["title"]);
		$con=htmlspecialchars($_POST["con"]);
		$cpc=$this->get_user_pv($buid);
		
		$catid=$_POST['catid'];
		if(!empty($_POST['tcatid']))
			$catid=$_POST['tcatid'];
		if(!empty($_POST['scatid']))
			$catid=$_POST['scatid'];
		if(!empty($_POST['sscatid']))
			$catid=$_POST['sscatid'];		
		$cat_name=$this->readCatName($catid);
		
		
		$sub_sql='';
		$pic=array();
		if(!empty($_POST['dele_pic'])||!empty($_POST['pic']))
		{	
			if(!$admin)
			{
				include_once("$config[webroot]/includes/admin_class.php");
				$admin = new admin();
			}
			$this->db->query("select pic from ".INFO." WHERE id='$_POST[editID]' and userid='$buid'");
			$pic = $this->db->fetchField('pic');
			$pic = $admin->update_pro_pic( $pic );
			$pic = $pic==NULL?'':implode(',',$pic);
			$sub_sql .= ",pic='$pic'";
		}

		$sql="update ".INFO." SET 
		catid='$catid',cat_name='$cat_name',title='$title',con='$con',
	    price='$_POST[price]',priceDes='$_POST[unit]',type='$_POST[type]',model='$_POST[model]',gg='$_POST[gg]',pp='$_POST[pinpai]',valid_time='$_POST[validTime]',keywords='$_POST[keywords]',country='$_POST[country]',province='$cpc[province]',city='$cpc[city]',company='$_POST[company]',ifpay='$ifpay',market_price='$_POST[market_price]',uptime='".$this -> cTime."'".$sub_sql." WHERE id='$_POST[editID]' and userid='$buid'";
		$re=$this->db->query($sql);
		if($re)
		{	
			//##==========================
			$sql="delete from  ".CUSTOM_CAT_REL." where relating_id=$_POST[editID] and custom_cat_type=2";//delete
			$this->db->query($sql);		
			if($_POST['custom_cat'])
			{
				$sql="insert into ".CUSTOM_CAT_REL." (custom_cat_id,custom_cat_type,relating_id) values 
				('$_POST[custom_cat]','2','$_POST[editID]')";
				$re=$this->db->query($sql);
			}	
			//##==========================
			$this->db->query("update ".OFFERDETAIL." set detail='$_POST[detail]' where offerid='$_POST[editID]'");
			include_once($config['webroot']."/includes/tag_inc.php");
			edit_tags($_POST['keyword'],2,$_POST['editID']);
		}
		return true;
	}
	function rec_pro($id,$type)
	{
		global $buid,$config;
		if($type=='rec')
		{	@session_start();
			//------------------
			include($config['webroot']."/config/usergroup.php");
			$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
			$my_group=$group[$ifpay]['modeu'];
			//-------------------
			$rec=$this->get_user_rec($buid);
			$rec=count($rec);
			if($rec<$my_group['rec_pro']+1)
			{
				if($rec>0)
					$sql="update ".SHOPSETTING." set rec_pro=REPLACE(rec_pro,',$id',''),rec_pro=concat(rec_pro,',$id') where userid='$buid'";
				else
					$sql="insert into ".SHOPSETTING." (userid,rec_pro,common_cat) values ('$buid',',$id','')";
				$re=$this->db->query($sql);
				if($re)
					return 1;
				else
					return 0;
			}
			else
				return 2;//超数量了
		}
		else
		{
			$sql="update ".SHOPSETTING." set rec_pro=REPLACE(rec_pro,',$id','') where userid='$buid'";
			$re=$this->db->query($sql);
			if($re)
				return 1;
			else
				return 0;
		}
	}
	function get_user_rec($id)
	{
		$sql="select id,rec_pro from ".SHOPSETTING." where userid='$id'";
		$this->db->query($sql);
		$rec=$this->db->fetchRow();
		if(!empty($rec['id']))
			return explode(",",$rec['rec_pro']);
		else
			return array();
	}
	function get_user_pv($userid)
	{
		$sql="select * from ".ALLUSER." where userid='$userid'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		foreach($re as $key=>$v)
		{
			if(!is_array($v))
				$re[$key]=addslashes($v);
		}
		return $re;

	}
	function info_list()
	{
		global $buid;
		$sql="select id,type,title,uptime,statu,pic from ".INFO." where userid='$buid' order by uptime desc";
		//=============================
	  	$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$this->db->query("select count(id) as num from ".INFO." where userid='$buid'");
			$num=$this->db->fetchRow();
			$page->totalRows = $num["num"];
		}
        $sql .= "  limit ".$page->firstRow.",20";
		//=====================
		$this->db->query($sql);
		while($pl=$this->db->fetchRow())
		{
			$pl['img'] = $this->get_cover_img($pl['pic'] );
			if( $pl['img']=='')
				$pl['nopic']=1;
			$re["list"][]=$pl;
		}
		$re["page"]=$page->prompt();
		return $re;
	}
	function space_offer_list($catid)
	{
		global $config;
		if(!empty($catid))
		{	
			$sql="select a.* from ".INFO." a left join ".CUSTOM_CAT_REL." b on a.id=b.relating_id and b.custom_cat_type=2 and b.custom_cat_id='$catid' where a.userid='$_GET[uid]' and a.statu>0 
				 order by uptime desc";
			$count_sql = "select count(*) as num from ".INFO." a left join ".CUSTOM_CAT_REL." b on a.id=b.relating_id and b.custom_cat_type=2 and b.custom_cat_id='$catid' where a.userid='$_GET[uid]' and a.statu>0";
		}
		else
		{
			$sql="select a.id,a.`type`,title,con,a.uptime,a.userid,a.user,pic  from ".INFO." a where a.userid='$_GET[uid]' order by id desc";
			$count_sql = "select count(*) as num from ".INFO." where userid='$_GET[uid]'";
		}
		//--------------------------------------------------
		include_once($config['webroot']."/includes/page_utf_class.php");
		$page = new Page;
		$page->url="shop.php";
		if (!$page->__get('totalRows')){
			$this->db->query($count_sql);
			$page->totalRows = $this->db->fetchField('num');
		}
		$sql .= "  limit ".$page->firstRow.", ".$page->listRows;
		//--------------------------------------------------
		$this ->db ->query($sql);
		$infoList['list'] = null;
		while($pl=$this ->db ->fetchRow())
		{
			$pl['img'] = $this->get_cover_img( $pl['pic'] );
			$infoList['list'][] = $pl;
		}
		$infoList['page']=$page->prompt();
		return $infoList;
	}
	function del_info($id,$uid=NULL)
	{
		global $buid,$config;
		if(!empty($uid))
			$buid=$uid;
			
		include($config['webroot']."/config/point_config.php");
		if($point_config['point']=='1'&&$point_config['del_offer']!='0')
			renew_point('',$point_config['del_offer']);
		
		$this->db->query("select catid,pic from ".INFO." where id='$id' and userid='$buid'");
		$re=$this->db->fetchRow();
		update_cat_nums($re['catid'],'del','offer');

		$this->db->query("delete from  ".INFO." where id='$id' and userid='$buid'");
		$this->db->query("delete from  ".OFFERDETAIL." where offerid='$id'");
		
		include_once($config['webroot']."/includes/tag_inc.php");
		del_tag($id,2);
		//删除图片
		if($re['pic']!='')
		{
			$pic = explode( ',',$re['pic'] );
			foreach($pic as $i)
			{
				@unlink($config['webroot'].'/'.$this->pic_save_path."$i");
				@unlink($config['webroot'].'/'.$this->pic_save_path.substr($i,0,strrpos($i,'/')+1).'big_'.substr($i,strrpos($i,'/')+1));
			}
		}
	}
	function update_info($id)
	{
		global $buid;
		if($id=="all")
		{
			include("config/point_config.php");
			if($point_config['point']=='1'&&$point_config['renew_offer']!='0')
			renew_point('',$point_config['renew_offer']);
			$sql="update ".INFO." SET uptime='$this->cTime' where userid='$buid'";
		}
		else
			$sql="update ".INFO." SET uptime='$this->cTime' where id='$id'";
		$this->db->query($sql);
	}

	function readCatName($id)
	{
		$this->db->query("select * from ".OCAT." where catid=$id");
		$re=$this->db->fetchRow();
		return $re["cat"];
	}
	function info_detail($id)
	{
		if($id)
		{
			global $config,$buid;
			include_once($config['webroot']."/includes/tag_inc.php");
			$sql="select a.*,b.detail from ".INFO." a left join ".OFFERDETAIL." b on a.id=b.offerid where a.id='$id' and b.userid='$buid'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			
			if(strlen($re['catid'])>8)
				$re['sscatid']=$re['catid'];
			
			if(strlen($re['catid'])>6)
				$re['scatid']=substr($re['catid'],0,8);
			
			if(strlen($re['catid'])>4)
				$re['tcatid']=substr($re['catid'],0,6);
			
			$re['catid']=substr($re['catid'],0,4);

			$sql="select custom_cat_id from ".CUSTOM_CAT_REL." where relating_id='$id' and custom_cat_type=2";
			$this->db->query($sql);
			$re['custom_cat_id'] =$this->db->fetchField('custom_cat_id');
			//===============================
			$BasePath = "lib/fckeditor/";
			include($config['webroot']."/$BasePath/fckeditor.php");	
			$fck_en = new FCKeditor('detail') ;
			$fck_en->BasePath    = $BasePath ;
			$fck_en->ToolbarSet  = 'frant' ;
			$fck_en->Width='100%';
			$fck_en->Height=500;
			$fck_en->Config['ToolbarStartExpanded'] = true;
			$fck_en->Value = stripslashes($re['detail']);
			$re['detail'] = $fck_en->CreateHtml();
			$re['keyword']=get_tags($id,2);
			//=================================
			if(!empty($re['pic'])){
				$pic = $this->get_cover_img( $prod['pic'] );
				$re['cover_big'] = substr($pic,0,strrpos($pic,'/')+1).'big_'.substr($pic,strrpos($pic,'/')+1);
				$re['pic'] = explode(",",$re['pic']);		
			}
			return $re;
		}
	}
	//--------------------------------------
	function company_other_offer($uid,$type=NULL,$limit=NULL)
	{
		if(!empty($limit))
			$sub_sql="limit 0,$limit";
		$sql="select title,id,type,pic from ".INFO." where userid='$uid' $sub_sql";
		$this->db->query($sql);
		while($pl=$this->db->fetchRow()){
			$pl['img'] = $this->get_cover_img($pl['pic']);
			$re[]= $pl;
		}
		return $re;
	}
	//========================
	function same_cat_offer($catid)
	{
		if(!empty($_POST['province']))
			$subsql=" and a.province='$_POST[province]'";

		$sql="select * from ".INFO." a left join ".USER." b on a.userid=b.userid
		 		where 
			  	a.catid='$catid' $subsql order by a.statu ,a.rank desc limit 10";		
		$this->db->query($sql);
		$samepro=$this->db->getRows();
		foreach($samepro as $k=>$v)
		{
			$samepro[$k]['pic']=explode(",",$v['pic']);
		}
		return $samepro;
	}

	//===================================================================
	function space_offer_detail($id)
	{
		if(is_numeric($id))
		{
			global $config;
			include("$config[webroot]/config/module_offer_config.php");
			$validTime=explode('|',$module_offer_config['validTime']);
			$infoType=explode('|',$module_offer_config['infoType']);
			
			$sql="select a.*,b.detail from ".INFO." a left join ".OFFERDETAIL." b on a.id=b.offerid 
			      where a.id='$_GET[id]'";
			$this->db->query($sql);
			$info=$this->db->fetchRow();
			$info['offertype']=$infoType[$info['type']];
			$info['valid_time']=$validTime[$info['valid_time']];
			
			if(!empty($info['pic']))
			{
				$pic = $this->get_cover_img( $info['pic'] );
				$info['cover'] = substr($pic,0,strrpos($pic,'/')+1).'big_'.substr($pic,strrpos($pic,'/')+1);
				$info['pic'] = explode(",",$info['pic']);		
			}
			return $info;
		}
		else
		{
			header("Location: 404.php");
		    exit();
		}
	}

	//##产品封面
	function get_cover_img( $pic='' ){
		if( $pic=='' )
			return NULL;
		$pic = explode( ',',$pic );
		foreach( $pic as $v ){
			if(file_exists($this->pic_save_path.$v))
				return $v;
		}
		return '';
	}
	//=======================================================
	function add_attach($offer_id)
	{
		global $config,$_OFFER_ATTACH,$buid;
		if(!empty($_POST['attach_file']))
		{
			include_once("$config[webroot]/module/download/includes/plugin_download_class.php");
			include_once("$config[webroot]/module/download/includes/plugin_download_class.php");
			$download = new download();
			$attachs = $_POST['attach_file'];

			$temp_dir = $config['webroot'].'/'.$_OFFER_ATTACH['upload_dir'].'/'.$_OFFER_ATTACH['temp_dir'];
			$ar=explode( '-',date('Y-m-d H:i:s') );
			$user_dir = $ar[0].'/'.$ar[1].'/'.$buid;
			$save_dir = $config['webroot'].'/'.$_OFFER_ATTACH['upload_dir'].'/'.$_OFFER_ATTACH['save_dir'].'/'.$user_dir; //附件目录
			mkdirs( $save_dir );

			$downs = array();
			foreach( $attachs as $key=>$v){
				$file['file_url'] = $attachs[$key]['url'];
				if( rename( $temp_dir.'/'.$file['file_url'],$save_dir.'/'.$file['file_url'] ) ){
					$file['userid'] = $buid;
					$file['offer_id'] = $offer_id;
					$file['downname'] = $attachs[$key]['name'];
					$file['body'] = $file['downname'];
					$file['pic'] =  preg_replace( '/.*\.(.*[^\.].*)*/iU','\\1',$file['file_url'] );
					if( in_array( $file['pic'],array('gif','jpg','jpeg','bmp','png')) ){
						@rename( $temp_dir.'/'.'mid_'.$file['file_url'],$save_dir.'/mid_'.$file['file_url'] );
					}
					$file['file_path'] = $user_dir;
					$file['file_size'] = $attachs[$key]['size'];
					$file['file_size'] = $file['file_size'] <1024?$file['file_size'].'KB':number_format( $file['file_size']/1024, 2, '.', '').'MB';
					$downs[] = $file;
				}
			}	
			if($downs!=null){
				$download->add_down( $downs );
			}
		}
	}
	//================================================
	function getProTypeName($prod)
	{
		if(!empty($prod))
		{
			$sql = "select cat from ".OCAT." where catid in($prod)";
			$this->db->query($sql);
			$fieldlist="";
			while($v=$this->db->fetchRow())
			{
				if($v["cat"]!="")
				$fieldlist.=$v["cat"]."->";
			}
			$fieldlist = trim($fieldlist,"->");
		}
		return $fieldlist;
	}

	function offer_merge_user($array)
	{
		$old_uid=$array['old_uid'];
		$new_uid=$array['new_uid'];

		$sql = "update ".INFO." set userid='$new_uid' where userid='$old_uid'";
		$this->db->query($sql);
		$sql = "update ".OFFERDETAIL." set userid='$new_uid' where userid='$old_uid'";
		$this->db->query($sql);
	}
}
?>
